(function(){
	window.code = null;
	window.selectedList = null;
	//window.nodePull = {};
	var codeBox = {
		conf : {
			'xmlpath' : "data/data.xml",
			'savepath' : "lib/saveCode.php",
			'state' : 0 //0:for browse code 1:for add new code
		},
		nodePull : {
		},
		init : function(){
			this.loadCode(this.conf.xmlpath);
			Co.get("saveCodeBtn").parentNode.style.display = "none";

			/*列表点击*/
			Co.addEvent(Co.get("codeList"),"change",this.showCode);
			/*分页点击*/
			Co.addEvent(Co.get("pageList"),"click",this.showPageListCode);
			/*搜索点击*/
			Co.addEvent(Co.get("searchCodeBtn"),"click",this.search);
			/*搜索回车*/
			Co.addEvent(Co.get("searchCodeIpt"),"keypress",function(event){
				var evt = Co.getEvent(event);
				var kc = evt.keyCode || evt.charCode;
				if(kc == 13){
					codeBox.search();
				}
			});
			/*添加新代码*/
			Co.addEvent(Co.get("newCode"),"click",this.addNewCode);

			/*运行代码*/
			Co.addEvent(Co.get("runCodeBtn"),"click",this.runCode);


			/*标题输入框keypress*/
			Co.addEvent(Co.get("codeTitle"),"keypress",function(event){
				var evt = Co.getEvent(event);
				var kc = evt.keyCode || evt.charCode;
				//document.title = kc;
				switch (kc)
				{
				case 32 ://space
					Co.stopEvent(evt);break;
				}
			});
			Co.addEvent(Co.get("codeTitle"),"keyup",function(event){
				//reg = /[\u4e00-\u9faf\s]+/;
				var val = this.value;
				if(/\W+/.test(val)){
					this.value = val.replace(/\W+/g,'');
				}
			});

			/*日期输入框focus*/
			Co.addEvent(Co.get("codePostTime"),"focus",function(){
				if(codeBox.getState()){
					var d = new Date();
					var str = d.getFullYear()
							+"-"+(d.getMonth()+1)
							+"-"+d.getDate();
					this.value = str;
				}
			});

			/*保存代码点击*/
			Co.addEvent(Co.get("saveCodeBtn"),"click",this.saveCode);
				
		},
		setState : function(stateCode){
			this.conf.state = stateCode;
		},
		getState : function(){
			return this.conf.state;
		},
		loadCode : function(path,fcallback){
			//载入data后回调
			var loadCodeList = function(){
				var xhr = this;
				var code = xhr.responseXML.documentElement;
				if(code != null)
				{
					window.code = code;
					var codeList = Co.get("codeList");
					codeList.options.length = 0;
					
					var nodes = code.childNodes, nodeName, tmpNode;
					var tmpDoc = document.createDocumentFragment();
					codeBox.nodePull = {};

					for(var i=0,il=nodes.length; i<il; i++){
						nodeName = nodes[i].nodeName;
						if(nodeName != "#text" && nodeName != "#comment" && !codeBox.nodePull[nodeName]){
							codeBox.nodePull[nodeName] = 1;
							tmpNode = document.createElement("option");
							tmpNode.innerHTML = nodeName;
							tmpNode.value = nodeName;
							tmpDoc.appendChild(tmpNode);
						}
					}
					codeList.appendChild(tmpDoc);
					codeBox.nodePull = null;//置空缓存器
					tmpDoc = null;

					/*执行回调*/
					if(fcallback){
						fcallback.call(xhr);
					}
				}
			}
			//载入XML列表
			Co.ajax.get(path,loadCodeList);
		},
		showCodeInfo:function(codeNode){
			Co.get("codeText").value = codeNode.firstChild.nodeValue;//兼容IE、FF的读取XML节点value
			Co.get("codeTitle").value = codeNode.tagName;
			Co.get("codeAuthor").value = codeNode.getAttribute("author");
			Co.get("codePostTime").value = codeNode.getAttribute("date");
		},
		showCode : function(){
			var codeTitle = this.value;
			var selectedCodeList = window.code.getElementsByTagName(codeTitle);
			var showCodeNode = selectedCodeList[0];
			var len = selectedCodeList.length;
			codeBox.showCodeInfo(showCodeNode);
			if(len > 1){
				//显示分页
				Co.get("pageInfo").innerHTML = "1/"+len;
				var tmpDoc = document.createDocumentFragment(),tmpNode;
				for(var i=0;i<len;i++)
				{
					tmpNode = document.createElement("li");
					tmpNode.innerHTML = i+1;
					tmpNode.title = i;
					tmpDoc.appendChild(tmpNode);
				}
				tmpDoc.firstChild.className = "currlist";
				Co.get("pageList").innerHTML = "";
				Co.get("pageList").appendChild(tmpDoc);

				window.selectedList = {
					node:selectedCodeList,
					idx:0
				}
			}else{
				window.selectedList = null;
				if(Co.get("pageInfo").innerHTML){
					Co.get("pageInfo").innerHTML = "";
					Co.get("pageList").innerHTML = "";
				} 
			}

			var saveCodeBtnWrap = Co.get("saveCodeBtn").parentNode;
			if("block" == saveCodeBtnWrap.style.display){
				saveCodeBtnWrap.style.display = "none";
			}
			/*记录状态*/
			codeBox.setState(0);
		},
		showPageListCode : function(event)
		{
			var evt = Co.getEvent(event);
			var target = evt.target;
			var gotoIdx = target.title;
			if("LI" == target.tagName 
				&& window.selectedList != null 
				&& window.selectedList.idx != gotoIdx){
				var showCodeNode = window.selectedList.node[gotoIdx];
				codeBox.showCodeInfo(showCodeNode);
				target.className = "currlist";
				Co.get("pageInfo").innerHTML = (parseInt(gotoIdx)+1) + "/" + window.selectedList.node.length;
				Co.get("pageList").childNodes[window.selectedList.idx].className="";
				window.selectedList.idx = gotoIdx;
			}
		},
		search : function(){
			var keyword = Co.get("searchCodeIpt").value;
			if(!keyword){
				return;
			}
			var codeList = Co.get("codeList");
			var selectIdx = codeList.selectedIndex,opt;
			var reg = new RegExp(keyword,"i");

			var len = codeList.options.length;
			var i = ((selectIdx+1)>=len) ? 0 : (selectIdx+1);

			for(var k=0; ;i++,k++){
				if(k>=len){
					return;
				}
				if(i>=len){
					i=0;
				}
				opt = codeList.options[i];
				if(reg.test(opt.value)){
					codeList.selectedIndex = i;
					codeBox.showCode.call(opt);//展示代码详细
					return;
				}
			}
		},
		addNewCode : function(){
			//Co.get("codeList").value = null;
			Co.get("codeText").value = "";
			Co.get("codeTitle").value = "";
			Co.get("codeAuthor").value = "";
			Co.get("codePostTime").value = "";

			Co.get("codeTitle").focus();
			/*显示保存按钮*/
			var saveCodeBtnWrap = Co.get("saveCodeBtn").parentNode;
			if("block" != saveCodeBtnWrap.style.display){
				saveCodeBtnWrap.style.display = "block";
			}
			/*隐藏分页*/
			if(Co.get("pageInfo").innerHTML){
				Co.get("pageInfo").innerHTML = "";
				Co.get("pageList").innerHTML = "";
				window.selectedList = null;
			}
			/*记录状态*/
			codeBox.setState(1);
		},
		runCode : function(){
			var code = Co.get("codeText").value;
			var win = window.open();
			win.document.write(code);
			win.document.close();
		},
		saveCode : function(){
			var title = Co.get("codeTitle").value.replace(/\s+/g,"");
			var author = Co.get("codeAuthor").value;
			var time = Co.get("codePostTime").value;
			var codeText = Co.get("codeText").value;
			if(!title || !codeText){
				return;
			}
			var value = "title=" + title
						+ (author ? ("&author="+encodeURIComponent(author)) : "" )
						+ (time ? ("&time="+encodeURIComponent(time)) : "")
						+ "&code="+encodeURIComponent(codeText);
			Co.ajax.post(codeBox.conf.savepath,value,function(){
				var xhr = this;
				if(parseInt(xhr.responseText)){
					//alert("保存成功");
					codeBox.loadCode(codeBox.conf.xmlpath+"?a="+Math.random(),function(){
						/*重新获取列表后，将左侧列表定位到刚添加的代码标题上*/
						var codeList = Co.get("codeList");
						for(var i=codeList.options.length-1;i>=0;i--){
							if(title == codeList.options[i].value){
								codeList.selectedIndex = i;
								codeList.value = title;
								codeBox.showCode.call(codeList.options[i]);//展示代码详细
								return;
							}
						}
					});
				}
			});
		}
		

	};

	codeBox.init();
})()